package com.heima.wemedia.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.heima.apis.article.IArticleClient;
import com.heima.model.comment.dtos.CommentPageDTo;
import com.heima.model.comment.dtos.CommentRepayDto;
import com.heima.model.comment.dtos.OpenOrCloseCommentDTO;
import com.heima.model.comment.pojos.ApComment;
import com.heima.model.comment.pojos.ApCommentLike;
import com.heima.model.comment.pojos.ApCommentRepay;
import com.heima.model.comment.pojos.ApCommentRepayLike;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.CommentManageService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author itlxc
 * @since 2024-07-31
 */
@Service
@Transactional
@RequiredArgsConstructor
public class CommentManageServiceImpl implements CommentManageService {

    private final MongoTemplate mongoTemplate;

    private final WmUserMapper wmUserMapper;

    private final IArticleClient articleClient;



    @Override
    public ResponseResult deleteComment(String commentId) {
        ApComment apComment = mongoTemplate.findById(commentId, ApComment.class);
        if (ObjectUtil.isNull(apComment)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        mongoTemplate.remove(apComment);
        //删除该评论的所有的回复内容
        mongoTemplate.remove(Query.query(Criteria.where("commentId").is(commentId)),ApCommentRepay.class);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    public ResponseResult deleteCommentRepay(String commentRepayId) {
        ApCommentRepay commentRepay = mongoTemplate.findById(commentRepayId, ApCommentRepay.class);
        if (ObjectUtil.isNull(commentRepay)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        mongoTemplate.remove(commentRepay);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult saveCommentRepay(CommentRepayDto dto) {
        // 1.参数校验
        if (ObjectUtil.isNull(dto.getCommentId()) || ObjectUtil.isNull(dto.getContent())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        ApUser apUser = AppThreadLocalUtil.getUser();
        // 2.封装数据
        ApCommentRepay apCommentRepay = BeanUtil.copyProperties(dto, ApCommentRepay.class);
        apCommentRepay.setAuthorId(apUser.getId());
        apCommentRepay.setLikes(0);
        apCommentRepay.setAuthorName(apUser.getName());
        apCommentRepay.setCreatedTime(new Date());
        apCommentRepay.setUpdatedTime(new Date());
        mongoTemplate.save(apCommentRepay);
        // 3.更新回复数量
        ApComment apComment = mongoTemplate.findById(dto.getCommentId(), ApComment.class);
        apComment.setReply(apComment.getReply() + 1);
        mongoTemplate.save(apComment);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult openOrCloseComment(OpenOrCloseCommentDTO dto) {
        WmUser wmUser = WmThreadLocalUtil.getUser();

        //app端用户id
        WmUser dbUser = wmUserMapper.selectById(wmUser.getId());
        Integer apUserId = dbUser.getApUserId();
        if (dto.getOperation() == 0) {
            //清空该文章的所有评论
            List<ApComment> apCommentList = mongoTemplate.find(Query.query(Criteria.where("entryId").is(dto.getArticleId()).and("authorId").is(apUserId)), ApComment.class);
            for (ApComment apComment : apCommentList) {
                List<ApCommentRepay> commentRepayList = mongoTemplate.find(Query.query(Criteria.where("commentId").is(apComment.getId()).and("authorId").is(apUserId)), ApCommentRepay.class);
                List<String> commentRepayIdList = commentRepayList.stream().map(ApCommentRepay::getId).distinct().collect(Collectors.toList());
                //删除所有的评论回复点赞数据
                mongoTemplate.remove(Query.query(Criteria.where("commentRepayId").in(commentRepayIdList).and("authorId").is(apUserId)), ApCommentRepayLike.class);

                //删除该评论的所有的回复内容
                mongoTemplate.remove(Query.query(Criteria.where("entryId").is(dto.getArticleId()).and("authorId").is(apUserId)), ApCommentRepay.class);

                //删除评论的点赞
                mongoTemplate.remove(Query.query(Criteria.where("commentId").is(apComment.getId()).and("authorId").is(apUserId)), ApCommentLike.class);
            }
            //删除评论
            mongoTemplate.remove(Query.query(Criteria.where("entryId").is(dto.getArticleId()).and("authorId").is(apUserId)),ApComment.class);
        }
        //修改app文章的config配置
        return articleClient.updateCommentStatus(dto);
    }


}
